import time
from selenium import webdriver
from selenium.webdriver.common.by import By
from pprint import pprint

# 打开游览器和QQ邮箱网页
driver = webdriver.Chrome()
driver.get('https://mail.qq.com/')
time.sleep(1)
"""
# 在开发者工具里，选中元素，点击Console，可以很直观的看到元素是否被iframe嵌套
# 找到登录页frame并切换
login = driver.find_element(By.ID,'login_frame')
driver.switch_to.frame(login)
# 找到账号输入框并输入
driver.find_element(By.ID,'u').send_keys('910****07@qq.com')
# 找到密码输入框并输入
driver.find_element(By.ID,'p').send_keys('ly134****3230')
# 找到登录按钮并点击
driver.find_element(By.ID,'login_button').click()
# 隐式等待加载
driver.implicitly_wait(10)
# 获取所有的cookie信息
cookies = driver.get_cookies()
pprint(cookies)
"""
# 准备好登录成功的cookies字典
cookies = [{'domain': '.mail.qq.com',
  'expiry': 1684302948,
  'httpOnly': False,
  'name': 'webp',
  'path': '/',
  'secure': False,
  'value': '1'},
 {'domain': '.mail.qq.com',
  'httpOnly': False,
  'name': 'new_mail_num',
  'path': '/',
  'secure': False,
  'value': '910862607&49'},
 {'domain': '.mail.qq.com',
  'httpOnly': False,
  'name': 'xm_sid',
  'path': '/',
  'secure': False,
  'value': 'zQ9BY4ypRlQuSmRuADZ1NQAA'},
 {'domain': '.mail.qq.com',
  'httpOnly': False,
  'name': 'xm_data_ticket',
  'path': '/',
  'secure': False,
  'value': '13102661935737103&CAESIE5fZHE6wwqTro2lzIYpNtXHtJ8GNj0xqqQxHB0XFrgO'},
 {'domain': '.mail.qq.com',
  'httpOnly': False,
  'name': 'username',
  'path': '/',
  'secure': False,
  'value': '910862607&910862607'},
 {'domain': '.mail.qq.com',
  'httpOnly': False,
  'name': 'xm_skey',
  'path': '/',
  'secure': False,
  'value': '13102661935737103&bc1b6e1b750f1acf4123406855fdc2c2'},
 {'domain': '.mail.qq.com',
  'httpOnly': False,
  'name': 'xm_muti_sid',
  'path': '/',
  'secure': False,
  'value': '13102661935737103&zQ9BY4ypRlQuSmRuADZ1NQAA'},
 {'domain': '.mail.qq.com',
  'httpOnly': False,
  'name': 'sid',
  'path': '/',
  'secure': False,
  'value': '910862607&f785ddc48ea828be9f2cfb22e4796c9e,qSUZvZGRNT3k2SGh4R1oqMU9CUWRwOXN3V3hvNHRqaWV0VVhrR2tXejN2SV8.'},
 {'domain': '.mail.qq.com',
  'httpOnly': False,
  'name': 'xm_uin',
  'path': '/',
  'secure': False,
  'value': '13102661935737103'},
 {'domain': '.qq.com',
  'httpOnly': False,
  'name': 'uin',
  'path': '/',
  'secure': False,
  'value': 'o910862607'},
 {'domain': '.mail.qq.com',
  'expiry': 1655358945,
  'httpOnly': False,
  'name': 'qm_logintype',
  'path': '/',
  'secure': False,
  'value': 'qq'},
 {'domain': '.mail.qq.com',
  'expiry': 1655358945,
  'httpOnly': False,
  'name': 'edition',
  'path': '/',
  'secure': False,
  'value': 'mail.qq.com'},
 {'domain': '.mail.qq.com',
  'httpOnly': False,
  'name': 'ssl_edition',
  'path': '/',
  'secure': False,
  'value': 'sail.qq.com'},
 {'domain': '.mail.qq.com',
  'httpOnly': False,
  'name': 'qm_domain',
  'path': '/',
  'secure': False,
  'value': 'https://mail.qq.com'},
 {'domain': '.mail.qq.com',
  'httpOnly': False,
  'name': 'pt4_token',
  'path': '/',
  'secure': False,
  'value': 'zTCajysTNFtrY-QP95LQ*peR5IBTn*y3f4qIYvp-sSI_'},
 {'domain': '.qq.com',
  'expiry': 2147483648,
  'httpOnly': False,
  'name': 'RK',
  'path': '/',
  'secure': False,
  'value': 'oJ8NZ87ERE'},
 {'domain': '.mail.qq.com',
  'httpOnly': False,
  'name': 'p_uin',
  'path': '/',
  'secure': False,
  'value': 'o0910862607'},
 {'domain': '.mail.qq.com',
  'expiry': 1684302945,
  'httpOnly': True,
  'name': 'xm_envid',
  'path': '/',
  'sameSite': 'Strict',
  'secure': True,
  'value': '456_B/xeVS4f4kAz2tXy8IT/h+uIkhm4JuaRhxeeLJ19JnN8nU2QoNw2BPJWVes7SY/YWbGEQhUblb8jXgF7SVsb23HiAzDfTfD/ivCgPog2ITpUm8Rc98IUxgAXQYRZr6ZTcvbvs7fu9mRijgsnKTh5sJXIhZ1f9Ekp094g/lTnjYNjv/jn3Q+ovew='},
 {'domain': '.mail.qq.com',
  'httpOnly': False,
  'name': 'qm_username',
  'path': '/',
  'secure': False,
  'value': '910862607'},
 {'domain': '.mail.qq.com',
  'expiry': 1655358945,
  'httpOnly': False,
  'name': 'qm_device_id',
  'path': '/',
  'secure': False,
  'value': 'oTbv3OQwNSP/ulhh8iglmjW5BJJWizRzMstOqLhCYQeZon1kzs5YSbe1YxnebIzE'},
 {'domain': '.qq.com',
  'expiry': 2147483648,
  'httpOnly': False,
  'name': 'ptcz',
  'path': '/',
  'secure': False,
  'value': '31588059df6ea135b0689343c8b23e3dc8962b4c43c29f92f067316c2faed567'},
 {'domain': '.qq.com',
  'httpOnly': False,
  'name': 'skey',
  'path': '/',
  'secure': False,
  'value': '@jrsY4YoVz'},
 {'domain': '.mail.qq.com',
  'httpOnly': False,
  'name': 'p_skey',
  'path': '/',
  'secure': True,
  'value': 'IFoddMOy6HhxGZ*1OBQdp9swWxo4tjietUXkGkWz3vI_'},
 {'domain': '.mail.qq.com',
  'expiry': 1655358947,
  'httpOnly': False,
  'name': 'CCSHOW',
  'path': '/',
  'secure': False,
  'value': '000000'},
 {'domain': '.qq.com',
  'expiry': 1655358945,
  'httpOnly': False,
  'name': 'ptui_loginuin',
  'path': '/',
  'secure': False,
  'value': '910862607@qq.com'}]

""""""
# 清空cookie，再进行添加
driver.delete_all_cookies()
time.sleep(3)

for cookie in cookies:
    driver.add_cookie(cookie)

time.sleep(9)
# 刷新页面查看登录后的界面
driver.refresh()
time.sleep(3)
""""""
# 找到写信并进入
driver.find_element(By.ID,'composebtn').click()

# 切换到iframe页面
driver.switch_to.frame('mainFrame')
time.sleep(2)

# 找到发件人输入邮箱
driver.find_element(By.ID,'toAreaCtrl').send_keys('619469654@qq.com')
# 找到主题并输入关键字
driver.find_element(By.ID,'subject').send_keys('周末')

# 找到正文的iframe进入
driver.switch_to.frame(driver.find_element(By.CLASS_NAME,'qmEditorIfrmEditArea'))
time.sleep(2)
# 找到正文并点击
driver.find_element(By.XPATH,'/html/body').click()
# 输入正文
driver.find_element(By.XPATH,'/html/body').send_keys('周末干嘛？')

# 返回父frame
driver.switch_to.parent_frame()
# 找到发送按钮并点击
driver.find_element(By.NAME,'sendbtn').click()

# 关闭游览器
time.sleep(2)
driver.quit()